
<template>
  <el-dialog v-model="dialogFormVisible" :title="title" width="1200">
    <el-form :model="form" ref="formRef" label-width="120" style="max-width: 1200px" :rules="rules">
      <div class="form-scroll">
        <el-row :gutter="20">

          <el-col :span="12">
            <el-form-item label="门派名称" prop="sectName">
              <el-input v-model="form.sectName" placeholder="请填写门派名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="门派封面图" :prop="sectUrl">
              <imageUpload v-model="form.sectUrl"></imageUpload>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="门派描述" prop="sectDesc">
              <el-input v-model="form.sectDesc" placeholder="请填写门派描述" />
            </el-form-item>
          </el-col>
        </el-row>
      </div>
    </el-form>
    <div>
      <el-button type="primary" @click="onSubmit">{{ isEdit? '编辑': '新增' }}</el-button>
      <el-button @click="dialogFormVisible = false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script setup>
    // 方法
    import { ref, defineExpose, defineEmits } from 'vue'
    const instance = getCurrentInstance();
    const { proxy } = instance || {};
    const { face_type } = proxy.useDict("face_type")
    import methods from '@/utils/methods.js'

    // 接口
    import { addSect, getSect, updateSect } from '@/api/jx3/sect.js'

    // 组件
    import imageUpload from '@/components/ImageUpload/index.vue'

    // 显示表单
    const dialogFormVisible = ref(false)
    // 弹窗标题
    const title = ref('新增门派')
    // 是否为编辑
    const isEdit = ref(false)
    // 表单节点
    const formRef = ref(null)
    // 表单
    const form = ref({
      id: undefined, // 唯一标识
      sectName: undefined, // 门派名称
      sectUrl: undefined, // 门派封面图
      sectDesc: undefined, // 门派描述
      createTime: undefined, // 创建时间
      createBy: undefined, // 创建人
      updateTime: undefined, // 更新时间
    })
    // 验证
    const rules = ref({
      // 门派名称
      sectName: [
        { required: true, message: '请填写门派名称', trigger: 'blur' }
      ],
      // 门派封面图
      sectUrl: [
        { required: true, message: '请上传门派封面图', trigger: 'blur' }
      ],
      // 门派描述
      sectDesc: [
        { required: true, message: '请填写门派描述', trigger: 'blur' }
      ],
      // 创建时间
      createTime: [
        { required: true, message: '请填写创建时间', trigger: 'blur' }
      ],
      // 创建人
      createBy: [
        { required: true, message: '请填写创建人', trigger: 'blur' }
      ],
      // 更新时间
      updateTime: [
        { required: true, message: '请填写更新时间', trigger: 'blur' }
      ],
    })

    const emits = defineEmits(['change'])

    // 打开方法
    const open = () => {
      for (let i in form.value) {
        form.value[i] = ''
      }
      dialogFormVisible.value = true
      isEdit.value = false
      title.value = '新增门派'
    }
    // 编辑
    const edit = (data) => {
      getSect(data.id).then(res => {
        for (let i in res.data) {
          form.value[i] = data[i]
        }
      })
      isEdit.value = true
      dialogFormVisible.value = true
      title.value = '编辑门派'
    }
    // 关闭方法
    const close = () => {
      dialogFormVisible.value = false
    }

    // 提交
    const onSubmit = async () => {
      await formRef.value.validate((valid, fields) => {
        if (valid) {
          const params = methods.deepClone(form.value)
          if(params.id) {
            updateSect(params).then(res => {
              if(res.code === 200) {
                proxy.$modal.msgSuccess(title.value + '成功');
                close()
                emits('change')
              }
            })
          }else {
            addSect(params).then(res => {
              if(res.code === 200) {
                proxy.$modal.msgSuccess(title.value + '成功');
                close()
                emits('change')
              }
            })
          }
        }
      })
    }

    defineExpose({
      open, close, edit
    })
</script>

<style scoped lang="scss">

</style>
